package controllers

import (
	mongodbutil "app/pkg/db"
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

type WebInfoController struct {
}

type WebInfo struct {
	Uid             string `json:"uid"`
	Name            string `json:"name"`
	Url             string `json:"url"`
	Weight          int32  `json:"weight"`
	UserId          string `json:"userId"`
	DelFlag         string `json:"delFlag"`
	WebCategoryId   string `json:"webCategoryId"`
	WebCategoryName string `json:"webCategoryName"`
}

// @Summary		List WebInfo
// @Description	get WebInfo
// @Tags			WebInfo
// @Accept			json
// @Produce		json
// @Param			q	query	string	false	"name search by q"	Format(email)
// @Success		200	{array}	WebInfo
// @Router			/web/getList [get]
func (u WebInfoController) GetList(c *gin.Context) {
	result := mongodbutil.GetList()
	Succeed(1, c, nil, 1, result)

}

// @Summary		List WebInfo
// @Description	post WebInfo
// @Tags			WebInfo
// @Accept			json
// @Produce		json
// @Param			webInfo	body	WebInfo	true	"请输入WebInfo information"
// @Success		200		{array}	WebInfo
// @Router			/web/SearchList [post]
func (u WebInfoController) SearchList(c *gin.Context) {
	var webInfo mongodbutil.WebInfo
	if err := c.ShouldBindJSON(&webInfo); err != nil {
		Failed(1, c, err.Error())
		return
	}
	result := mongodbutil.SearchList(webInfo)

	Succeed(1, c, result, 1, nil)

}

// @Summary		List WebInfo
// @Description	post WebInfo
// @Tags			WebInfo
// @Accept			json
// @Produce		json
// @Param			webCategoryName	query	string	false	"webCategoryName"
// @Param			page			query	int		false	"页码，默认为 1"
// @Param			limit			query	int		false	"每页数量，默认为 10"
// @Success		200				{array}	WebInfo
// @Router			/web/GetSearchList [get]
func (u WebInfoController) GetSearchList(c *gin.Context) *gin.Context {
	var webInfo mongodbutil.WebInfo
	webInfo.WebCategoryName = c.Query("webCategoryName")
	result := mongodbutil.SearchList(webInfo)

	Succeed(1, c, result, 1, nil)
	return c
}

// @Summary		上传文件
// @Description	上传文件
// @Tags			WebInfo
// @Accept			multipart/form-data
// @Produce		json
// @Param			file	formData	file	true	"请选择文件"
// @Success		200		{object}	string
// @Router			/web/UploadFile [post]
func (u WebInfoController) UploadFile(c *gin.Context) {
	// 获取普通文本字段
	name := c.PostForm("name")
	// 获取文件
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 1, "message": "获取数据失败"})
	} else {
		// 保存上传的文件
		c.SaveUploadedFile(file, "upload/"+file.Filename)
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "上传成功", "name": name})
	}
}

// @Summary		下载文件
// @Description	下载文件
// @Tags			WebInfo
// @Accept			json
// @Produce		json
// @Param			content	query		string	true	"文件名"
// @Success		200		{object}	string
// @Router			/web/DownloadFile [get]
func (u WebInfoController) DownloadFile(c *gin.Context) {
	content := c.Query("content")

	content = "hello world, 我是一个文件，" + content

	c.Writer.WriteHeader(http.StatusOK)
	c.Header("Content-Disposition", "attachment; filename=hello.txt")
	c.Header("Content-Type", "application/text/plain")
	c.Header("Accept-Length", fmt.Sprintf("%d", len(content)))
	c.Writer.Write([]byte(content))
}
